package view
{
	import com.apdevblog.utils.URLUtils;
	
	import events.ReaderEvent;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.external.ExternalInterface;
	import flash.filters.DropShadowFilter;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import model.ReaderModel;
	
	public class ControlBar extends Sprite
	{
		private var _bg:Sprite;
		
		private var _m:ReaderModel = ReaderModel.getInstance();
		
		private var _cont:Sprite;
		
		private var _cont_page:Sprite;
		
		private var _t_total_page:TextField;
		private var _t_current_page:TextField;
		
		private var _btnPre:ReaderButton;
		private var _btnNext:ReaderButton;
		private var _btnPrinter:ReaderButton;
		private var _btnScaleL:ReaderButton;
		private var _btnScaleS:ReaderButton;
		private var _btnDoc:ReaderButton;
		private var _btnThumbV:ReaderButton;
		private var _btnFullScreen:ReaderButton;
		private var _btnNormalScreen:ReaderButton;
		
		private var _hasExtraViewScale:Boolean = false;
		
		private var _print:PrintPopup;
		
		private var _logo:Sprite;
		
		public function ControlBar()
		{
			if(stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void
		{
			if(hasEventListener(Event.ADDED_TO_STAGE)) removeEventListener(Event.ADDED_TO_STAGE, init);
			initUI();
			addListeners();
			layout();	
//			_hasExtraViewScale = stage.stagew
			this.visible = true;
			onRefreshButtonStatus();
		}
		
		public function layout():void
		{
			_bg.graphics.clear();
			_bg.graphics.beginFill(_m.style.top_bar_bg_color);
			_bg.graphics.drawRect(0,0,stage.stageWidth, 44);
			_bg.graphics.endFill();
			
//			_cont.x = stage.stageWidth - 418;
			_cont.x = stage.stageWidth - 234;
			_cont.y = 7;
			
//			_cont_page.x = stage.stageWidth * 0.5 - 80;
			
			var halfSW:Number = stage.stageWidth * 0.5;
			if(halfSW - 331 < 0)
			{
				_cont_page.x = stage.stageWidth - 331 - 80;
			}else{
				_cont_page.x = halfSW - 80;
			}
			
			_cont_page.y = 7;
			
			_print.layout();
			_btnFullScreen.visible = stage.displayState == StageDisplayState.NORMAL;
			_btnNormalScreen.visible = stage.displayState == StageDisplayState.FULL_SCREEN;
		}
		
		public function setPageInfo(current:int):void
		{
			_m.current_page = current;
			_t_total_page.text = "/" + _m.total_page_num;
			_t_current_page.text = String(current);
		}
		
		private function addListeners():void
		{
			_btnThumbV.addEventListener(MouseEvent.CLICK, onBtnThumbVClick);
			_btnDoc.addEventListener(MouseEvent.CLICK, onBtnDocClick);
			_btnScaleL.addEventListener(MouseEvent.CLICK, onBtnScaleLClick);
			_btnScaleS.addEventListener(MouseEvent.CLICK, onBtnScaleSClick);
			_m.addEventListener(ReaderEvent.VIEW_SCALE_CHANGED, onViewScaleChanged);
			_m.addEventListener(ReaderEvent.VIEW_MODE_CHANGED, onViewModeChanged);
			_m.addEventListener(ReaderEvent.REFRESH_BUTTON_STATUS, onRefreshButtonStatus);
			_btnFullScreen.addEventListener(MouseEvent.CLICK, onBtnFullScreenClick);
			_btnNormalScreen.addEventListener(MouseEvent.CLICK, onBtnNormalScreenClick);
			_btnPre.addEventListener(MouseEvent.CLICK, onPreClick);
			_btnNext.addEventListener(MouseEvent.CLICK, onNextClick);
			_btnPrinter.addEventListener(MouseEvent.CLICK, onBtnPrinterClick);
			_btnPrinter.visible = _m.style.show_print;
			_t_current_page.addEventListener(FocusEvent.FOCUS_IN, onCurrentPageFocuseIn);
			_t_current_page.addEventListener(FocusEvent.FOCUS_OUT, onCurrentPageFocuseOut);
		}
		
		protected function onRefreshButtonStatus(e:ReaderEvent = null):void
		{
			if(_m.viewMode == 0)
			{
				_btnScaleL.enabled = !(_m.scaleID == _m.style.scales.length - 1);
				_btnScaleS.enabled = !(_m.scaleID == 0);
			}else{
				_btnScaleL.enabled = !(_m.thumbCountsID == 0);
				_btnScaleS.enabled = !(_m.thumbCountsID == _m.style.thumbCounts.length - 1);
			}
		}
		
		protected function onBtnPrinterClick(e:MouseEvent):void
		{
			_m.block = true;
			_print.show(this.stage);
		}
		
		protected function onNextClick(e:MouseEvent):void
		{
			_m.jumpToPage(_m.current_page + 1);
		}
		
		protected function onPreClick(e:MouseEvent):void
		{
			_m.jumpToPage(_m.current_page - 1);
		}
		
		protected function onBtnFullScreenClick(e:MouseEvent):void
		{
//			this.stage.displayState = (this.stage.displayState == StageDisplayState.FULL_SCREEN) ? StageDisplayState.NORMAL : StageDisplayState.FULL_SCREEN;
			this.stage.displayState = StageDisplayState.FULL_SCREEN;
		}
		
		protected function onBtnNormalScreenClick(event:MouseEvent):void
		{
			this.stage.displayState = StageDisplayState.NORMAL;
		}
		
		protected function onBtnThumbVClick(event:MouseEvent):void
		{
			_m.viewMode = 1;		
		}
		
		protected function onBtnDocClick(event:MouseEvent):void
		{
			_m.viewMode = 0;
		}
		
		protected function onViewModeChanged(event:Event):void
		{
			_btnThumbV.selected = (_m.viewMode == 1);
			_btnDoc.selected = (_m.viewMode == 0);
			
			_btnPre.enabled = _btnNext.enabled = (_m.viewMode == 0);
			if(_m.viewMode == 0)
			{
				_cont_page.graphics.lineStyle(1,0xd5d5d5);
				_cont_page.graphics.beginFill(0xffffff);
				_cont_page.graphics.drawRect(36, 0, 63, 29);
				_cont_page.graphics.endFill();
				_t_current_page.type = TextFieldType.INPUT;
				_t_current_page.mouseEnabled = true;
			}else{
				_cont_page.graphics.lineStyle(1,0xd5d5d5);
				_cont_page.graphics.beginFill(_m.style.btn_disable_color, 0.5);
				_cont_page.graphics.drawRect(36, 0, 63, 29);
				_cont_page.graphics.endFill();
				_t_current_page.type = TextFieldType.DYNAMIC;
				_t_current_page.mouseEnabled = false;
			}
		}
		
		protected function onViewScaleChanged(event:ReaderEvent):void
		{
			
		}
		
		protected function onBtnScaleSClick(event:MouseEvent):void
		{
			_m.scaleDown();
		}
		
		protected function onBtnScaleLClick(event:MouseEvent):void
		{
			_m.scaleUp();
		}
		
		private function initUI():void
		{
			this.visible = false;
			_bg = new Sprite();
			addChild(_bg);
			
			_bg.filters = [new DropShadowFilter(1,90, 0xcacaca, 0.38, 0, 0)];
			
			_btnPre = new ReaderButton(new SKbtnPre(), {over: _m.style.btn_over_color,
				out: _m.style.btn_out_color, down: _m.style.btn_down_color, disable: _m.style.btn_disable_color});
			
			_btnNext = new ReaderButton(new SKbtnNext(), {over: _m.style.btn_over_color,
				out: _m.style.btn_out_color, down: _m.style.btn_down_color, disable: _m.style.btn_disable_color});
			
			_cont_page = new Sprite();
			
			_cont_page.graphics.lineStyle(1,0xd5d5d5);
			_cont_page.graphics.beginFill(0xffffff);
			_cont_page.graphics.drawRect(36, 0, 63, 29);
			_cont_page.graphics.endFill();
			
			_t_total_page = new TextField();
			_t_total_page.text = "/0";
			_t_total_page.x = 104;
			_t_total_page.y = 7;
			_t_total_page.autoSize = TextFieldAutoSize.LEFT;
			var tf1:TextFormat =new TextFormat("Arial", 12);
			_t_total_page.defaultTextFormat = tf1;
			_t_total_page.setTextFormat(tf1);
			
			_t_current_page = new TextField();
			_t_current_page.x = 40;
			_t_current_page.y = 7;
//			_t_current_page.autoSize = TextFieldAutoSize.LEFT;
			_t_current_page.type = TextFieldType.INPUT;
			_t_current_page.width = 63;
			_t_current_page.height = 29;
			_t_current_page.restrict = "0-9";
			_t_current_page.multiline = false;
			_t_current_page.defaultTextFormat = tf1;
			_t_current_page.setTextFormat(tf1);
			
			_cont_page.addChild(_t_current_page);
			_cont_page.addChild(_t_total_page);
			
			addChild(_cont_page);
			
			_cont = new Sprite();
			
			_cont_page.addChild(_btnPre);
			_btnNext.x = 132;
			_cont_page.addChild(_btnNext);
			
			_btnPrinter = new ReaderButton(new SKbtnPrinter(), {over: _m.style.btn_over_color,
				out: _m.style.btn_out_color, down: _m.style.btn_down_color, disable: _m.style.btn_disable_color});
//			_btnPrinter.x = _btnNext.x + 23 + _btnPrinter.width;
			_cont.addChild(_btnPrinter);
			
			_btnScaleL = new ReaderButton(new SKbtnScaleL(), {over: _m.style.btn_over_color,
				out: _m.style.btn_out_color, down: _m.style.btn_down_color, disable: _m.style.btn_disable_color});
			_btnScaleL.x = _btnPrinter.x + 17 + _btnPrinter.width;
			_cont.addChild(_btnScaleL);
			
			_btnScaleS = new ReaderButton(new SKbtnScaleS(), {over: _m.style.btn_over_color,
				out: _m.style.btn_out_color, down: _m.style.btn_down_color, disable: _m.style.btn_disable_color});
			_btnScaleS.x = _btnScaleL.x + _btnScaleL.width - 1;
			_cont.addChild(_btnScaleS);
			
			_btnDoc = new ReaderButton(new SKbtnDoc(), {over: _m.style.btn_over_color,
				out: _m.style.btn_out_color, down: _m.style.btn_down_color, disable: _m.style.btn_disable_color});
			_btnDoc.x = _btnScaleS.x + _btnScaleS.width + 17;
			_btnDoc.selected = true;
			_cont.addChild(_btnDoc);
			
			_btnThumbV = new ReaderButton(new SKthumbV(), {over: _m.style.btn_over_color,
				out: _m.style.btn_out_color, down: _m.style.btn_down_color, disable: _m.style.btn_disable_color});
			_btnThumbV.x = _btnDoc.x + _btnDoc.width - 1;
			_cont.addChild(_btnThumbV);
			
			_btnFullScreen = new ReaderButton(new SKfullScreen(), {over: _m.style.btn_over_color,
				out: _m.style.btn_out_color, down: _m.style.btn_down_color, disable: _m.style.btn_disable_color});
			_btnFullScreen.x = _btnThumbV.x + _btnThumbV.width + 17;
			_cont.addChild(_btnFullScreen);
			
			_btnNormalScreen = new ReaderButton(new SKnormalScreen(), {over: _m.style.btn_over_color,
				out: _m.style.btn_out_color, down: _m.style.btn_down_color, disable: _m.style.btn_disable_color});
			
			_btnNormalScreen.x = _btnFullScreen.x;
			_cont.addChild(_btnNormalScreen);
//			_btnNormalScreen.visible = false;			
			addChild(_cont);
			
			_logo = new Sprite();
			_logo.x = _m.style.logo_x;
			_logo.y = _m.style.logo_y;
			_logo.buttonMode = true;
			_logo.addEventListener(MouseEvent.CLICK, onLogoClick);
			addChild(_logo);
			
			var ld:Loader = new Loader();
			ld.load(new URLRequest(_m.style.logo_uri));
			_logo.addChild(ld);
			
			_print = new PrintPopup();
		}
		
		protected function onLogoClick(event:MouseEvent):void
		{
//			if(ExternalInterface.available)  ExternalInterface.call("window.open", _m.style.logo_link, '_self');
//			navigateToURL(new URLRequest(_m.style.logo_link), '_self');
			URLUtils.openWindow(_m.style.logo_link, "_self");
		}
		
		protected function onCurrentPageFocuseOut(e:FocusEvent):void
		{
			_m.block = false;
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			_t_current_page.removeEventListener(Event.CHANGE, onTextInputChange);
			_t_current_page.text = String(_m.current_page);
			stage.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
		}
		
		protected function onKeyDown(e:KeyboardEvent):void
		{
			if(e.keyCode == 13)
			{
				var tid:int = int(_t_current_page.text);
				if(tid < 1) tid = 1;
				if(tid > _m.total_page_num) tid = _m.total_page_num;
				_m.jumpToPage(tid);
				stage.focus = null;
			}
		}
		
		protected function onCurrentPageFocuseIn(e:FocusEvent):void
		{
			_m.block = true;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			_t_current_page.addEventListener(Event.CHANGE, onTextInputChange);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
		}
		
		protected function onMouseWheel(e:MouseEvent):void
		{
			stage.focus = null;
		}
		
		protected function onTextInputChange(e:Event):void
		{
			if(_t_current_page.text == "") return;
			var tid:int = int(_t_current_page.text);
			if(tid < 1) tid = 1;
			if(tid > _m.total_page_num) tid = _m.total_page_num;
			_t_current_page.text = String(tid);
		}
		
	}
	
}